home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks97 / WarriorsProgress.sit / Warrior’s Progress / source code / Source / Libraries / Sequence / SequenceLoop.cp < prev    next >
Text File  |  1997-06-28  |  6KB  |  314 lines

  1. // SequenceLoop.cp
  2.  
  3. #ifndef SequenceLoop_h
  4. #include "SequenceLoop.h"
  5. #endif
  6. #ifndef Sequence_h
  7. #include "Sequence.h"
  8. #endif
  9.  
  10. template < class Head, class Node >
  11. SequenceLoop<Head,Node>::SequenceLoop( const Head& list )
  12.   : position( list.First() ),
  13.      next( 0 ),
  14.      previous( 0 ),
  15.      head( list ),
  16.      nextLoop( 0 )
  17.   {
  18.     finished = ( position == 0 );
  19.     list.Register( *this );
  20.   }
  21.  
  22. template < class Head, class Node >
  23. SequenceLoop<Head,Node>::SequenceLoop( const Head& list, AtStart )
  24.   : position( list.First() ),
  25.      next( 0 ),
  26.      previous( 0 ),
  27.      head( list ),
  28.      nextLoop( 0 )
  29.   {
  30.     finished = ( position == 0 );
  31.     list.Register( *this );
  32.   }
  33.  
  34. template < class Head, class Node >
  35. SequenceLoop<Head,Node>::SequenceLoop( const Head& list, AtEnd )
  36.   : position( list.Last() ),
  37.      next( 0 ),
  38.      previous( 0 ),
  39.      head( list ),
  40.      nextLoop( 0 )
  41.   {
  42.     finished = ( position == 0 );
  43.     list.Register( *this );
  44.   }
  45.  
  46. template < class Head, class Node >
  47. SequenceLoop<Head,Node>::SequenceLoop( const Head& list, Nowhere )
  48.   : position( 0 ),
  49.      next( 0 ),
  50.      previous( 0 ),
  51.      head( list ),
  52.      nextLoop( 0 ),
  53.      finished( true )
  54.   {
  55.     list.Register( *this );
  56.   }
  57.  
  58. template < class Head, class Node >
  59. SequenceLoop<Head,Node>::SequenceLoop( const Head& list,
  60.                                              const Node& thePosition )
  61.   : position( &thePosition ),
  62.      next( 0 ),
  63.      previous( 0 ),
  64.      head( list ),
  65.      nextLoop( 0 ),
  66.      finished( false )
  67.   {
  68.     Assert( thePosition.Owned() );
  69.     Assert( &head == &thePosition.Owner() );
  70.     list.Register( *this );
  71.   }
  72.  
  73. template < class Head, class Node >
  74. SequenceLoop<Head,Node>::SequenceLoop( const Head& list,
  75.                                              Before,
  76.                                              const Node& thePosition )
  77.   : position( 0 ),
  78.      next( &thePosition ),
  79.      previous( thePosition.Previous() ),
  80.      head( list ),
  81.      nextLoop( 0 ),
  82.      finished( false )
  83.   {
  84.     Assert( thePosition.Owned() );
  85.     Assert( &head == &thePosition.Owner() );
  86.     list.Register( *this );
  87.   }
  88.  
  89. template < class Head, class Node >
  90. SequenceLoop<Head,Node>::SequenceLoop( const Head& list,
  91.                                              After,
  92.                                              const Node& thePosition )
  93.   : position( 0 ),
  94.      next( thePosition.Next() ),
  95.      previous( &thePosition ),
  96.      head( list ),
  97.      nextLoop( 0 ),
  98.      finished( false )
  99.   {
  100.     Assert( thePosition.Owned() );
  101.     Assert( &head == &thePosition.Owner() );
  102.     list.Register( *this );
  103.   }
  104.  
  105. template < class Head, class Node >
  106. SequenceLoop<Head,Node>::SequenceLoop( const Head& list, BeforeStart )
  107.   : position( 0 ),
  108.      next( list.First() ),
  109.      previous( 0 ),
  110.      head( list ),
  111.      nextLoop( 0 ),
  112.      finished( false )
  113.   {
  114.     list.Register( *this );
  115.   }
  116.  
  117. template < class Head, class Node >
  118. SequenceLoop<Head,Node>::SequenceLoop( const Head& list, AfterEnd )
  119.   : position( 0 ),
  120.      next( 0 ),
  121.      previous( list.Last() ),
  122.      head( list ),
  123.      nextLoop( 0 ),
  124.      finished( false )
  125.   {
  126.     list.Register( *this );
  127.   }
  128.  
  129. template < class Head, class Node >
  130. SequenceLoop<Head,Node>::SequenceLoop( const Loop& source )
  131.   : position( source.position ),
  132.      next( source.next ),
  133.      previous( source.previous ),
  134.      head( source.head ),
  135.      nextLoop( 0 ),
  136.      finished( source.finished )
  137.   {
  138.     head.Register( *this );
  139.   }
  140.  
  141. template < class Head, class Node >
  142. SequenceLoop<Head,Node>::~SequenceLoop()
  143.   {
  144.     head.Unregister( *this );
  145.   }
  146.  
  147. template < class Head, class Node >
  148. void SequenceLoop<Head,Node>::MoveToFinish()
  149.   {
  150.     position = 0;
  151.     previous = 0;
  152.     next = 0;
  153.     finished = true;
  154.   }
  155.  
  156. template < class Head, class Node >
  157. void SequenceLoop<Head,Node>::MoveToFirst()
  158.   {
  159.     position = head.First();
  160.     next = 0;
  161.     previous = 0;
  162.     finished = (position == 0);
  163.   }
  164.  
  165. template < class Head, class Node >
  166. void SequenceLoop<Head,Node>::MoveToLast()
  167.   {
  168.     position = head.Last();
  169.     next = 0;
  170.     previous = 0;
  171.     finished = (position == 0);
  172.   }
  173.  
  174. template < class Head, class Node >
  175. void SequenceLoop<Head,Node>::MoveTo( const Node& thePosition )
  176.   {
  177.     Assert( thePosition.Owned() );
  178.     Assert( &head == &thePosition.Owner() );
  179.     
  180.     position = &thePosition;
  181.     next = 0;
  182.     previous = 0;
  183.     finished = false;
  184.   }
  185.  
  186. template < class Head, class Node >
  187. void SequenceLoop<Head,Node>::MoveBefore( const Node& thePosition )
  188.   {
  189.     Assert( thePosition.Owned() );
  190.     Assert( &head == &thePosition.Owner() );
  191.     
  192.     position = 0;
  193.     next = &thePosition;
  194.     previous = thePosition.Previous();
  195.     finished = false;
  196.   }
  197.  
  198. template < class Head, class Node >
  199. void SequenceLoop<Head,Node>::MoveAfter( const Node& thePosition )
  200.   {
  201.     Assert( thePosition.Owned() );
  202.     Assert( &head == &thePosition.Owner() );
  203.     
  204.     position = 0;
  205.     next = thePosition.Next();
  206.     previous = &thePosition;
  207.     finished = false;
  208.   }
  209.  
  210. template < class Head, class Node >
  211. void SequenceLoop<Head,Node>::MoveBeforeFirst()
  212.   {
  213.     position = 0;
  214.     next = head.First();
  215.     previous = 0;
  216.     finished = false;
  217.   }
  218.  
  219. template < class Head, class Node >
  220. void SequenceLoop<Head,Node>::MoveAfterLast()
  221.   {
  222.     position = 0;
  223.     next = 0;
  224.     previous = head.Last();
  225.     finished = false;
  226.   }
  227.  
  228. template < class Head, class Node >
  229. void SequenceLoop<Head,Node>::operator=( const Loop& source )
  230.   {
  231.     Assert( &head == &source.head );
  232.     
  233.     position = source.position;
  234.     next = source.next;
  235.     previous = source.previous;
  236.     finished = source.finished;
  237.   }
  238.  
  239. template < class Head, class Node >
  240. bool SequenceLoop<Head,Node>::operator==( const Loop& right ) const
  241.   {
  242.     Assert( &head == &right.head );
  243.     
  244.     return position == right.position
  245.          && previous == right.previous
  246.          && next == right.next
  247.          && finished == right.finished;
  248.   }
  249.  
  250. template < class Head, class Node >
  251. bool SequenceLoop<Head,Node>::operator==( const Node& right ) const
  252.   {
  253.     Assert( right.Owned() );
  254.     Assert( &head == &right.Owner() );
  255.     
  256.     return position == &right;
  257.   }
  258.  
  259. template < class Head, class Node >
  260. const Node *SequenceLoop<Head,Node>::Next() const
  261.   {
  262.     Assert( Unfinished() );
  263.  
  264.     if ( position == 0 )
  265.         return next;
  266.      else
  267.         return position->Next();
  268.   }
  269.  
  270. template < class Head, class Node >
  271. const Node *SequenceLoop<Head,Node>::Previous() const
  272.   {
  273.     Assert( Unfinished() );
  274.  
  275.     if ( position == 0 )
  276.         return previous;
  277.      else
  278.         return position->Previous();
  279.   }
  280.  
  281. template < class Head, class Node >
  282. void SequenceLoop<Head,Node>::operator++()
  283.   {
  284.     Assert( Unfinished() );
  285.     
  286.     if ( position == 0 )
  287.       {
  288.         position = next;
  289.         next = 0;
  290.         previous = 0;
  291.       }
  292.      else
  293.         position = position->Next();
  294.     
  295.     finished = ( position == 0 );
  296.   }
  297.  
  298. template < class Head, class Node >
  299. void SequenceLoop<Head,Node>::operator--()
  300.   {
  301.     Assert( Unfinished() );
  302.     
  303.     if ( position == 0 )
  304.       {
  305.         position = previous;
  306.         next = 0;
  307.         previous = 0;
  308.       }
  309.      else
  310.         position = position->Previous();
  311.     
  312.     finished = ( position == 0 );
  313.   }
  314.